﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace MusicForms
{
    public partial class Form2 : Form
    {
        static Division division1 = new Division();
        List<AbstractMusic> music1;
        AbstractMusic track;
        Form1 form1;

        public Form2(Form1 fr1, Division division, List<AbstractMusic> music)
        {
            InitializeComponent();
            this.Text = "Добавление трека";
            form1 = fr1;
            music1 = music;
            division1 = division;
            button3.Visible = false;
            comboBox1.Items.Add("Метал");
            comboBox1.Items.Add("Альтернатива");
            comboBox1.Items.Add("Рэп");
            Label[] labelBox = { label2, label3, label4, label5, label6, label7, label8, label9 };
            TextBox[] textBoxBox = { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8 };
            for (int i = 0; i < 8; i++)
            {
                labelBox[i].Visible = false;
                textBoxBox[i].Visible = false;
            }
        }

        public Form2(Form1 fr1, Division division, List<AbstractMusic> music, int selectedIndex)
        {
            InitializeComponent();
            this.Text = "Редактирование трека";
            form1 = fr1;
            music1 = music;
            division1 = division;
            button1.Visible = false;
            comboBox1.Visible = false;
            label1.Visible = false;
            Label[] labelBox = { label4, label5, label6, label7, label8, label9 };
            TextBox[] textBoxBox = { textBox3, textBox4, textBox5, textBox6, textBox7, textBox8 };
            for (int i = 0; i < 6; i++)
            {
                labelBox[i].Visible = false;
                textBoxBox[i].Visible = false;
            }
            track = music[selectedIndex];
            if (track!=null)   
            {
                textBox1.Text = track.Name;
                textBox2.Text = Convert.ToString(track.Duration / 60) + ':' + Convert.ToString(track.Duration % 60);
            }
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label[] labelBox = { label2, label3, label4, label5, label6, label7, label8, label9 };
            TextBox[] textBoxBox = { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8 };
            for (int i = 0; i < 8; i++)
            {
                labelBox[i].Visible = false;
                textBoxBox[i].Visible = false;
            }
            if (comboBox1.Text == "Метал")
            {
                for (int i = 0; i < 6; i++)
                {
                    if (i == 4) i++;
                    labelBox[i].Visible = true;
                    textBoxBox[i].Visible = true;
                }
            }
            if (comboBox1.Text == "Альтернатива")
            {
                for (int i = 0; i < 5; i++)
                {
                    labelBox[i].Visible = true;
                    textBoxBox[i].Visible = true;
                }
            }
            if (comboBox1.Text == "Рэп")
            {
                for (int i = 0; i < 8; i++)
                {
                    if (i == 2 ) i += 4;
                    labelBox[i].Visible = true;
                    textBoxBox[i].Visible = true;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Метал")
            {
                Metal metal = new Metal();
                metal.Name = textBox1.Text;
                metal.Duration = Convert.ToInt16(textBox2.Text.Split(':')[0]) * 60 + Convert.ToInt16(textBox2.Text.Split(':')[1]);
                metal.BandName = textBox3.Text;
                metal.Year = Convert.ToInt16(textBox4.Text);
                metal.NumberOfGuitarists = Convert.ToInt16(textBox6.Text);
                division1.AddMusic(metal);
            }
            if (comboBox1.Text == "Альтернатива")
            {
                Alternative alt = new Alternative();
                alt.Name = textBox1.Text;
                alt.Duration = Convert.ToInt16(textBox2.Text.Split(':')[0]) * 60 + Convert.ToInt16(textBox2.Text.Split(':')[1]);
                alt.BandName = textBox3.Text;
                alt.Year = Convert.ToInt16(textBox4.Text);
                alt.VocalistName = textBox5.Text;
                division1.AddMusic(alt);
            }
            if (comboBox1.Text == "Рэп")
            {
                Rap rap = new Rap();
                rap.Name = textBox1.Text;
                rap.Duration = Convert.ToInt16(textBox2.Text.Split(':')[0]) * 60 + Convert.ToInt16(textBox2.Text.Split(':')[1]);
                rap.SingerName = textBox7.Text;
                rap.Language = textBox8.Text;
                division1.AddMusic(rap);
            }
            form1.listBox1.Items.Clear();
            foreach (AbstractMusic track in music1)
            {
                form1.listBox1.Items.Add("'" + track.Name + "' - " + track.Duration / 60 + ':' + track.Duration % 60);
            }
            form1.UpdateLength();
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            track.Name = textBox1.Text;
            track.Duration = Convert.ToInt16(textBox2.Text.Split(':')[0]) * 60 + Convert.ToInt16(textBox2.Text.Split(':')[1]);
            form1.listBox1.Items.Clear();
            foreach (AbstractMusic track1 in music1)
            {
                form1.listBox1.Items.Add("'" + track1.Name + "' - " + track1.Duration / 60 + ':' + track1.Duration % 60);
            }
            form1.UpdateLength();
            Close();
        }
    }
}
